package start.demo01.fileIO;

import java.io.*;

public class FileNameFilter {
    public static void main(String[] args) throws IOException {
//        String path="./";
//        File file=new File(path);
        //list文件过滤
//        String[] names=file.list((dir,name)-> name.endsWith(".xsx") || name.endsWith(".txt"));
//        for(String name:names)
//            System.out.println(name);
        byte[] bytes={(byte)231,(byte)148,(byte)181,(byte)232,(byte)132,(byte)145};

        //readFile("./das.txt");
        //readFile(new File("./das.txt"));
        //testData();
        InputStreamReader reader=new InputStreamReader(new BufferedInputStream(new FileInputStream("das.txt")));
        BufferedReader br=new BufferedReader(reader);
        String line=null;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
        reader.close();
    }
    private static class SuffixFilenameFilter implements FilenameFilter{
        public boolean accept(File dir,String name){
            return name.endsWith(".xsx") || name.endsWith(".txt");
        }
    }

    public static void readFile(String filename) throws IOException {
        File file=new File(filename);
        System.out.println(file.length()+"B");
        InputStream in=new FileInputStream(file);
        int val=-1;
        while((val=in.read())!=-1){
            System.out.println(val);
        }
        in.close();
    }
    public static void readFileString(String filename){

    }
    public static void readFile(File file){
        try(InputStream in=new FileInputStream(file)) {
            byte[] buffer=new byte[10];//缓冲区,太小会乱码
            int ret=-1;
            while((ret=in.read(buffer))!=-1){//没读完返回读取的字节长度
                System.out.println(new String(buffer,0,ret));
                //new String(buffer,0,ret,"UTF-8");指定解码方式
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void copyFile(File src,File dist){
        try(InputStream in=new FileInputStream(src);
            OutputStream out=new FileOutputStream(dist);) {
            byte[] buffer=new byte[1024];
            int len=0-1;
            while((len=in.read(buffer))!=-1){
                out.write(buffer,0,len);
                //System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void copyFile(String src,String target){//带有缓存字符流，可制定缓存大小
        try(BufferedReader reader=new BufferedReader(new FileReader(src));
            BufferedWriter writer=new BufferedWriter(new FileWriter(target))) {
            String line=null;//reader.readLine();
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();//写入换行
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /*
    * 按照流的角色分
    * 节点流:从/向特定的设备 读/写的流
    * 处理流:对一个存在的流封装
    * */
    public static void testData(){
        try(DataOutputStream out=new DataOutputStream(new FileOutputStream(new File("das.txt")))) {
            out.writeUTF("夏莉莉");
            out.writeDouble(88.32);
            out.writeUTF("java");
            out.writeInt(3);
            out.writeUTF("2019-10-30");

        } catch (IOException e) {
            e.printStackTrace();
        }
        try(DataInputStream in=new DataInputStream(new FileInputStream(new File("das.txt")))) {
            System.out.println(in.readUTF()+":"+in.readDouble());
            System.out.println(in.readUTF()+":"+in.readInt());
            System.out.println(in.readUTF());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
